06. JavaScript Demo
So you saw how to use
JavaScript demo
So you saw how to use console.log
to print a message to the JavaScript console. Now, let’s see how you can use the console as a sandbox to test a new line of JavaScript in the browser.
Open the following site in a new tab and in that tab also open up developer tools. Then paste the following code:
document.getElementsByTagName("h1")[0].style.color = "#ff0000";
JavaScript and the Web Quiz
SOLUTION:
The heading changed to redStyling elements of the page
Styling elements on the page is great, but you could also do that by just modifying the CSS. What makes JavaScript so special in this case? Refresh the page, then paste this line of code in the JavaScript console.
document.body.addEventListener('click', function () {
var myParent = document.getElementById("Banner");
var myImage = document.createElement("img");
myImage.src = 'https://thecatapi.com/api/images/get?format=src&type=gif';
myParent.appendChild(myImage);
myImage.style.marginLeft = "160px";
});
If you’re confused because nothing happened. Don’t worry. Click somewhere on the page.
JavaScript and the Web Quiz (Follow-up)
SOLUTION:
An image is added to the page17_Img_gif The Working Site (BETA)
INSTRUCTOR NOTE:
When you click, cat gifs will be added to the webpage 🐱 !